package main.java.DMDE.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import main.java.DMDE.controllers.factories.IOperationControllerFactory;
import main.java.DMDE.controllers.listeners.IEntityEventListener;
import main.java.DMDE.infrastructure.IProjectContext;
import main.java.DMDE.model.Attribute;
import main.java.DMDE.model.Interface;
import main.java.DMDE.model.Method;
import main.java.DMDE.view.IInterfaceView;

public class InterfaceController extends BaseController implements IInterfaceController {
    
    private IInterfaceView interfaceView;
    private Interface pendingInterface;
    private List<IEntityEventListener> listeners;
    private IOperationControllerFactory operationControllerFactory;
    private IOperationController operationController;
    
    public InterfaceController(IProjectContext projectContext,
	    IOperationControllerFactory operationControllerFactory,
	    IInterfaceView interfaceView) {
	super(projectContext);
	this.interfaceView = interfaceView;
	this.interfaceView.setController(this);
	this.listeners = new ArrayList<IEntityEventListener>();
	this.pendingInterface = new Interface("", UUID.randomUUID());
	this.operationControllerFactory = operationControllerFactory;
    }

    @Override
    public void create(String entityName) {
	this.operationController = this.operationControllerFactory.create();
	this.pendingInterface.setName(entityName);
	this.interfaceView.setInterfaceName(entityName);
	this.interfaceView.addOperationView(operationController.getOperationView());
	this.interfaceView.showView();
    }

    @Override
    public void addSubscriber(IEntityEventListener listener) {
	this.listeners.add(listener);	
    }

    @Override
    public boolean addInterface() {
	String interfaceName = this.interfaceView.getInterfaceName();
	this.pendingInterface.setName(interfaceName);
	List<Method> methods = this.operationController.getMethods();
	this.pendingInterface.setMethods(methods);
	for (IEntityEventListener listener : this.listeners) {
	    listener.handleCreatedEvent(pendingInterface);
	}
	return true;
    }

    @Override
    public void create(Interface entity) {
	this.pendingInterface = entity;
	this.operationController = this.operationControllerFactory.create();
	List<Method> operations = this.pendingInterface.getMethods();
	this.operationController.setOperations(operations);
	this.interfaceView.addOperationView(operationController.getOperationView());
	this.interfaceView.setInterfaceName(this.pendingInterface.getName());
	this.interfaceView.setModeUpdating();
	this.interfaceView.showView();
    }

}
